package com.wze.wze.Controller;

import com.wze.wze.Aop.PasToken;
import com.wze.wze.Entity.Friend;
import com.wze.wze.Entity.ResultEntity;
import com.wze.wze.Service.FriendService;
import com.wze.wze.Util.JSONTool;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin //解决跨域
@RestController //控制器注解
@RequestMapping("/Friend/")//请求路径
public class FriendController {
    @Resource
    FriendService friendService;
    ResultEntity resultEntity = null;


    @PostMapping("Friend")
    private String addFriend( @RequestParam("uid") int uid,@RequestParam("FriendName") String FriendName) {
        resultEntity = new ResultEntity();
        try {
            if (friendService.addFriend(uid,FriendName)>0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }



    @DeleteMapping("Friend")
    private String delFriend(@RequestParam("FriendName") String FriendName) {
        resultEntity = new ResultEntity();
        try {
            if (friendService.delFriendByFn(FriendName) > 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }


    @PasToken
    @GetMapping("FriendList")
    private String PlList(
            @RequestParam("page") int page,
            @RequestParam("limit") int limit,
            @RequestParam("uid")  int uid,
            @RequestParam("FriendName") String FriendName
    ){
        try {
            resultEntity = new ResultEntity();
            int Start = (page-1) * limit;//计算分页位置
            List<Friend> list = friendService.getFriendListByuid(Start,limit,uid,FriendName);
            resultEntity.setCode("0");
            resultEntity.setData(list);
            resultEntity.setMsg("查询成功!");
        }catch (Exception e){
            resultEntity.setCode("1");
            resultEntity.setMsg("查询失败!"+e.getMessage());
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }
}
